package LeetCode.leetcode701;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        //首个节点判断一下
        if (root == null)
            return new TreeNode(val);
        //如果val的值比root的值小 --> 左边
        if (root.val > val){
            if (root.left == null){
                root.left = new TreeNode(val);
            }else {
                insertIntoBST(root.left, val);
            }
        }else {
            if (root.right == null){
                root.right = new TreeNode(val);
            }else {
                insertIntoBST(root.right,val);
            }
        }
        return root;
    }
}
